﻿using System;
using System.Data;
using System.Reflection;
using l1cs.server.model;
using l1cs.server.model.Instance;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;

namespace l1cs.server.datatables
{
    /// <summary>
    /// アジトの家具クラス
    /// </summary>
    public class FurnitureSpawnTable
    {
        private static Logger _log = Logger.getLogger( typeof( FurnitureSpawnTable ).FullName );

        private static FurnitureSpawnTable _instance;

        public static FurnitureSpawnTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new FurnitureSpawnTable();
            }
            return _instance;
        }

        private FurnitureSpawnTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM spawnlist_furniture";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    int npcid = SqlUtils.toInt( item["npcid"] );
                    L1Npc l1npc = NpcTable.getInstance().getTemplate( npcid );
                    if ( l1npc == null )
                    {
                        continue;
                    }
                    if ( l1npc.getImpl() == "L1Furniture" )
                    {
                        L1FurnitureInstance furniture = new L1FurnitureInstance( l1npc );
                        furniture.setId( IdFactory.getInstance().nextId() );
                        furniture.setItemObjId( SqlUtils.toInt( item["item_obj_id"] ) );
                        furniture.setX( SqlUtils.toInt( item["locx"] ) );
                        furniture.setY( SqlUtils.toInt( item["locy"] ) );
                        furniture.setMap( SqlUtils.toShort( item["mapid"] ) );
                        furniture.setHomeX( furniture.getX() );
                        furniture.setHomeY( furniture.getY() );
                        furniture.setHeading( 0 );

                        L1World.getInstance().storeObject( furniture );
                        L1World.getInstance().addVisibleObject( furniture );
                    }
                }
            }
        }

        /// <summary>
        /// 家具を追加する。
        /// </summary>
        /// <param name="furniture"></param>
        public void insertFurniture( L1FurnitureInstance furniture )
        {/*TTOK*/
            String sqlstr = @"INSERT INTO spawnlist_furniture SET 
                                item_obj_id=?1, 
                                npcid      =?2, 
                                locx       =?3, 
                                locy       =?4, 
                                mapid      =?5";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = furniture.getItemObjId();
                cmd.Parameters.Add( "?2", MySqlDbType.Int32 ).Value = furniture.getNpcTemplate().get_npcId();
                cmd.Parameters.Add( "?3", MySqlDbType.Int32 ).Value = furniture.getX();
                cmd.Parameters.Add( "?4", MySqlDbType.Int32 ).Value = furniture.getY();
                cmd.Parameters.Add( "?5", MySqlDbType.Int32 ).Value = furniture.getMapId();
                wrapper.ExecNonQuery( cmd );
            }
        }

        /// <summary>
        /// 家具を削除する。
        /// </summary>
        /// <param name="furniture"></param>
        public void deleteFurniture( L1FurnitureInstance furniture )
        {/*TTOK*/
            String sqlstr = "DELETE FROM spawnlist_furniture WHERE item_obj_id=?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = furniture.getItemObjId();
                wrapper.ExecNonQuery( cmd );
            }
        }
    }
}
